package com.hao.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hao.eduservice.entity.EduCourse;
import com.hao.eduservice.entity.EduCourseDescription;
import com.hao.eduservice.entity.frontvo.CourseFrontVo;
import com.hao.eduservice.entity.frontvo.CourseWebVo;
import com.hao.eduservice.entity.vo.CourseInfoVo;
import com.hao.eduservice.entity.vo.CoursePublishVo;
import com.hao.eduservice.mapper.EduCourseMapper;
import com.hao.eduservice.service.EduChapterService;
import com.hao.eduservice.service.EduCourseDescriptionService;
import com.hao.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hao.eduservice.service.EduVideoService;
import com.hao.servicebase.exceptionhandler.GuliException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author hao
 * @since 2020-12-02
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService descriptionService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;


    //使用setter方式注入,避免Field injection is not recommended警告
    //public EduCourseDescriptionService descriptionService;
    //
    //@Autowired
    //public void setSysRoleService(EduCourseDescriptionService descriptionService) {
    //    this.descriptionService = descriptionService;
    //}

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        boolean isSave = this.save(eduCourse);
        if(!isSave){
            throw new GuliException(20001,"课程添加失败");
        }
        String id = eduCourse.getId();
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,description);
        description.setId(id);
        descriptionService.save(description);

        return id;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        EduCourse course = this.getById(courseId);
        EduCourseDescription description = descriptionService.getById(courseId);
        CourseInfoVo vo = new CourseInfoVo();
        BeanUtils.copyProperties(course,vo);
        BeanUtils.copyProperties(description,vo);
        return vo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,course);
        boolean b = this.updateById(course);
        if(!b){
            throw new GuliException(20001,"课程修改失败");
        }
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,description);
        boolean b1 = descriptionService.updateById(description);
        if(!b1){
            throw new GuliException(20001,"课程修改失败");
        }
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(courseId);
        return publishCourseInfo;
    }

    @Override
    public void deleteCourse(String courseId) {
        //根据课程id删除小节
        videoService.removeByCourseId(courseId);
        //根据课程id删除章节
        chapterService.removeByCourseId(courseId);
        //根据课程id删除课程描述
        boolean b1 = descriptionService.removeById(courseId);
        if(!b1){
            throw new GuliException(20001,"删除课程描述失败");
        }
        //根据课程id删除课程自己
        boolean b = this.removeById(courseId);
        if(!b){
            throw new GuliException(20001,"删除课程失败");
        }

    }

    @Cacheable(key = "'selectFrontCourse'",value = "course")
    @Override
    public List<EduCourse> selectFrontCourse() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 8");
        List<EduCourse> list = this.list(wrapper);
        return list;
    }

    @Override
    public List<EduCourse> getCourseInfoByTeacherId(long teacherId) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id",teacherId);
        return this.list(wrapper);
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //拼接查询条件
        //一级分类
        if(StringUtils.isNotEmpty(courseFrontVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        //二级分类
        if(StringUtils.isNotEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        //拼接查询排序
        //关注度
        if(StringUtils.isNotEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        //时间
        if(StringUtils.isNotEmpty(courseFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }
        //价格
        if(StringUtils.isNotEmpty(courseFrontVo.getPriceSort())){
            wrapper.orderByDesc("price");
        }
        IPage<EduCourse> courseIPage = baseMapper.selectPage(pageParam, wrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }

}
